{include file="common/header"/}
<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body">
          <div class="layui-row">
              <form action="" class="layui-form">
                  <div class="layui-row layui-col-space10">
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="wx_id" id="wx_id">
                                {volist name="wechat" id="vo"}
                                <option value="{$vo.id}">{$vo.name}</option>
                                {/volist}
                            </select>
                        </div>
                        <input type="button" id="searchBtn" name="submit" class="layui-btn" value="搜索">
                    </div>
                  </div>
              </form>
          </div>
          <div class="layui-row">
              <table class="layui-table" id="list" lay-filter="list"></table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-group">
        <a href="javascript:;" class="layui-btn layui-btn-sm" onclick="open_frame('添加','{:url('add')}');"><i class="layui-icon layui-icon-add-circle"></i>{:lang('add')}菜单</a>
        <a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-normal" onclick="saveMenu();">保存及发布</a>
        <a href="javascript:;" class="layui-btn layui-btn-normal layui-btn-sm" onclick="openAll();"><i class="layui-icon layui-icon-reduce-circle"></i>展开或折叠全部</a>
    </div>
    
</script>


<script type="text/html" id="action">
    <div class="layui-btn-group">
        <a class="layui-btn layui-btn-xs" onclick="open_frame('编辑','{:url('edit')}?id={{d.id}}')">{:lang('edit')}</a>
        <a class="layui-btn layui-btn-xs layui-btn-warm"  onclick="open_frame('添加','{:url('add')}?parentid={{d.id}}')">{:lang('add')}{:lang('child')}</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs"  data-href="{:url('delete')}" lay-event="del">{:lang('del')}</a>
    </div>
</script>
<script type="text/html" id="order">
    <input name="{{d.id}}" data-id="{{d.id}}" class="layui-input list_order" value=" {{d.sort}}" size="10"/>
</script>
<script type="text/html" id="create_time">
    {{layui.util.toDateString(d.createtime*1000, 'yyyy-MM-dd HH:mm:ss')}}
</script>

{include file="common/footer"/}
<script>
var tableIn=null, treeGrid=null,tableId='list';
var searchObj = {};
searchObj['wechat_id'] = $('#wx_id option:selected').val();
layui.extend({
    treeGrid: 'lib/treeGrid/treeGrid',
}).use(['jquery','treeGrid','layer','form'], function(){
    var $=layui.jquery ,form = layui.form;
    treeGrid = layui.treeGrid;
    layer=layui.layer;
    tableIn=treeGrid.render({
        id:tableId
        ,elem: '#'+tableId
        ,idField:'id'
        ,url:'{:url("index")}'
        ,where:searchObj
        ,toolbar: '#toolbarDemo'
        ,cellMinWidth: 100
        ,treeId:'id'//树形id字段名称
        ,treeUpId:'pid'//树形父id字段名称
        ,treeShowName:'title'//以树形式显示的字段
        ,height:'full-140'
        ,isFilter:false
        ,iconOpen:true//是否显示图标【默认显示】
        // ,isOpenDefault:true//节点默认是展开还是折叠【默认展开】
        ,cols: [[
            {field: 'id', title: 'ID', width: 70, fixed: true},
            {field: 'title', title: '菜单名称', width: 200},
            {field: 'menu_event', title: '事件', minwidth: 200},
            {field: 'sort', title: '排序', minwidth: 200, templet: '#order'},
            {field: 'createtime',align: 'center',title: '创建时间', templet: '#create_time'},
            {title:'操作',width:200, toolbar: '#action',align:"center"},
        ]]
        ,page:false
    });
    // 搜索
    $('#searchBtn').click(function(){
        searchObj['title'] = $('#title').val();
        table.reload('list',{where:searchObj});
    }) 

    $('body').on('blur','.list_order',function() {
       var id = $(this).attr('data-id');
       var sort = $(this).val();
       $.post('{:url("menuOrder")}',{id:id,sort:sort},function(res){
            if(res.code==1){
                layer.msg(res.msg,{time:1000,icon:1},function(){
                    location.href = res.url;
                });
            }else{
                layer.msg(res.msg,{time:1000,icon:2});
                window.location.reload();
            }
       })
    })
});
function openAll() {
    var treedata=treeGrid.getDataTreeList(tableId);
    treeGrid.treeOpenAll(tableId,!treedata[0][treeGrid.config.cols.isOpen]);
}
function saveMenu(){
    $.ajax({
        url:"{:url('setMenu')}",
        dataType:"json",
        data:{"wx_id":$('#wx_id option:selected').val()},  
        type:"post",
        success:function(req){
        	if (req.code == 1) {
        		layer.msg(req.msg);
        	}else{
        		layer.msg('保存失败');
        	}
            return false;
        },
        error:function(){
            layer.msg('请求出错');return false;
        }
    })
}
</script>